//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Product template manager
    /// </summary>
    public class ProductTemplateManager
    {
        #region Constants
        private const string PRODUCTTEMPLATES_ALL_KEY = "Nop.producttemplate.all";
        private const string PRODUCTTEMPLATES_BY_ID_KEY = "Nop.producttemplate.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Deletes a product template
        /// </summary>
        /// <param name="ProductTemplateID">Product template identifier</param>
        public static void DeleteProductTemplate(int ProductTemplateID)
        {
            ProductTemplateDBManager.DeleteProductTemplate(ProductTemplateID);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTTEMPLATES_ALL_KEY);
                NopCache.RemoveByPattern(PRODUCTTEMPLATES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets all product templates
        /// </summary>
        /// <returns>Product template collection</returns>
        public static ProductTemplateCollection GetAllProductTemplates()
        {
            string key = string.Format(PRODUCTTEMPLATES_ALL_KEY);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductTemplateCollection)obj2;
            }

            ProductTemplateCollection productTemplateCollection = ProductTemplateDBManager.GetAllProductTemplates();
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productTemplateCollection);
            }
            return productTemplateCollection;
        }

        /// <summary>
        /// Gets a product template
        /// </summary>
        /// <param name="ProductTemplateID">Product template identifier</param>
        /// <returns>Product template</returns>
        public static ProductTemplate GetByProductTemplateID(int ProductTemplateID)
        {
            string key = string.Format(PRODUCTTEMPLATES_BY_ID_KEY, ProductTemplateID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductTemplate)obj2;
            }

            ProductTemplate productTemplate = ProductTemplateDBManager.GetByProductTemplateID(ProductTemplateID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productTemplate);
            }
            return productTemplate;
        }

        /// <summary>
        /// Inserts a product template
        /// </summary>
        /// <param name="Name">The name</param>
        /// <param name="TemplatePath">The template path</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Product template</returns>
        public static ProductTemplate InsertProductTemplate(string Name, string TemplatePath,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            ProductTemplate productTemplate = ProductTemplateDBManager.InsertProductTemplate(Name, TemplatePath,
             DisplayOrder, CreatedOn, UpdatedOn);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTTEMPLATES_ALL_KEY);
                NopCache.RemoveByPattern(PRODUCTTEMPLATES_BY_ID_KEY);
            }

            return productTemplate;
        }

        /// <summary>
        /// Updates the product template
        /// </summary>
        /// <param name="ProductTemplateID">The product template identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="TemplatePath">The template path</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Product template</returns>
        public static ProductTemplate UpdateProductTemplate(int ProductTemplateID, string Name, string TemplatePath,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            ProductTemplate productTemplate = ProductTemplateDBManager.UpdateProductTemplate(ProductTemplateID, Name, TemplatePath,
             DisplayOrder, CreatedOn, UpdatedOn);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTTEMPLATES_ALL_KEY);
                NopCache.RemoveByPattern(PRODUCTTEMPLATES_BY_ID_KEY);
            }

            return productTemplate;
        }
        #endregion
    }
}
